package com.fgs.common.uploadPicture;

import android.content.Context;
import android.text.TextUtils;

import com.fgs.common.CommonModel;
import com.fgs.common.CommonResponse;
import com.fgs.common.api.CommonApiService;
import com.fgs.common.api.CommonConfig;
import com.fgs.common.entity.PictureInfo;
import com.fgs.common.entity.UploadPictureInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.uploadPicture.compress.CompressController;
import com.fgs.common.uploadPicture.compress.OnPictureCompressListener;
import com.fgs.common.utils.LogUtil;
import com.fgs.common.utils.ToastUtil;
import com.xuexiang.xutil.net.JsonUtil;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @author fgs
 * @date 2020/10/17
 * @description
 **/
public class UploadPicture implements IUploadPicture {

    private final UploadPictureCallback uploadPictureCallback;
    private CommonModel commonModel;


    public UploadPicture(UploadPictureCallback callback) {
        uploadPictureCallback = callback;
    }

    @Override
    public void uploadPicture(Context context, UploadPictureInfo uploadPictureInfo) {

        if (commonModel == null) {
            commonModel = new CommonModel();
        }
        if (uploadPictureInfo != null) {
            File file;
            if (!TextUtils.isEmpty(uploadPictureInfo.picturePath)) {
                file = new File(uploadPictureInfo.picturePath);
            } else {
                file = uploadPictureInfo.file;
            }

            if (file == null) return;

            compressPicture(context, file, uploadPictureInfo);

        }


    }

    //上传前二次压缩
    private void compressPicture(Context context, File file, UploadPictureInfo uploadPictureInfo) {
        LogUtil.print("UploadPicture file size--" + file.length() / 1024 + "k");
        //图片大小>600K，进行二次压缩
//        boolean isNeedSecondCompress = file.length() / 1024 > 400;
        boolean isNeedSecondCompress = file.length() / 1024 > 600;

        if (isNeedSecondCompress) {
            CompressController.getInstance().pictureCompress(file, new OnPictureCompressListener() {

                @Override
                public void onSuccess(File file) {
                    requestUploadPicture(context, uploadPictureInfo, file);
                }

                @Override
                public void onError(Throwable e) {

                }
            });
        } else {
            requestUploadPicture(context, uploadPictureInfo, file);
        }
    }


    private void requestUploadPicture(Context context, UploadPictureInfo uploadPictureInfo, File file) {
        LogUtil.print("图片大小", file.length() / 1024 + "k");

        //1.创建MultipartBody.Builder对象
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        RequestBody body = RequestBody.create(file, MediaType.parse("multipart/form-data"));//表单类型

        builder.addFormDataPart("file", file.getName(), body); //添加图片数据，body创建的请求体
        builder.addFormDataPart("module", uploadPictureInfo.pictureModule); //feedback
        List<MultipartBody.Part> parts = builder.build().parts();


        commonModel.subscribe(context, true, CommonConfig.getInstance().create(CommonApiService.class).uploadPicture(parts), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    PictureInfo pictureInfo = JsonUtil.fromJson(JsonUtil.toJson(commonResponse.getData()), PictureInfo.class);
                    if (uploadPictureCallback != null) {
                        uploadPictureCallback.onSuccess(uploadPictureInfo, pictureInfo);
                    }
                } else {
                    ToastUtil.showShortToast(commonResponse.getMsg());
                    if (uploadPictureCallback != null) {
                        uploadPictureCallback.onFailed(uploadPictureInfo);
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                ToastUtil.showShortToast(e.message);
                if (uploadPictureCallback != null) {
                    uploadPictureCallback.onFailed(uploadPictureInfo);
                }
            }
        });
    }

}
